<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%!
	// 当日危急值数量
	String criticalCount(Integer hospital, Integer uid, String today) {
		today =  " and concat(FROM_UNIXTIME(nc.cTime/1000,'%Y-%m-%d'), '') = '" + today + "'";
		List<Integer> hospitals = QBuilder.db("basedb").queryHql("select parent from Hospital where hospital=" + hospital );
		if (isEmpty(hospitals)) {
			return "0";
		}
		hospital = hospitals.get(0);	
		List<Integer> uids = QBuilder.db("basedb").queryHql("select id from User where userName = (select employeeId from User where id=" + uid + " ) and hospital=" + hospital);
		if (isEmpty(uids)) {
			return "0";
		}
		uid = uids.get(0);
		String hql = "select concat(count(*), '') from  NotificationConfirm as nc where  nc.uid=" + uid  + " and nc.hospital=" + hospital + " and nc.status=" + STATUS_ENABLE + today ;
		List<String> notifictionList = QBuilder.db("basedb").queryHql(hql);
		return isEmpty(notifictionList)?"0":notifictionList.get(0);
	}
	
	// 当日日程数量 
	String arrangeCount(Integer hospital, Integer uid, String today) {
		List<String> arrangeList = QBuilder.db("basedb").select("concat(count(partner), '')").from("Arrangement").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("DATE_FORMAT(start,'%Y-%m-%d')=", today).and("partner=", uid)).group("partner").query().getResultObj();
		return isEmpty(arrangeList)?"0":arrangeList.get(0);
	}
	
	// 当日生成订单量(未处理)
	String serviceOrderCount(Integer hospital, Integer uid) {
		List<String> orderList = QBuilder.db("basedb").select("concat(count(*), '')").from("Order").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("doctor=", uid).and("state=", 1)).group("doctor").query().getResultObj();
		return isEmpty(orderList)?"0":orderList.get(0);
	}
	
%>
<%
	Integer uid = token.getUid();
	String today = DateUtil.formatDate(new Date());
	Map<String, String> indexMap = new HashMap<String, String>();
	indexMap.put("critical_value", criticalCount(hospital, uid, today));
	indexMap.put("arrangement", arrangeCount(hospital, uid, today));
	indexMap.put("service_order", serviceOrderCount(hospital, uid));
	msg.put("data", indexMap);
	Tools.encryptPrint(out, msg);
%>
